﻿using System;
using Cirrious.MvvmCross.Exceptions;
using Cirrious.MvvmCross.ExtensionMethods;
using Cirrious.MvvmCross.Interfaces.Platform;
using Cirrious.MvvmCross.Views;
using Cirrious.MvvmCross.WindowsPhone.ExtensionMethods;
using Cirrious.MvvmCross.WindowsPhone.Views;
using Microsoft.Phone.Controls;
using TeaLightV5.Foundation.MvvmCross.Views;

namespace TeaLightV5.Foundation.Ux.WinPhone.MvvmCross.Views
{
    public class TeaPhoneViewsContainer : MvxPhoneViewsContainer
    {
        public TeaPhoneViewsContainer(PhoneApplicationFrame frame) : base(frame)
        {
        }

        private const string QueryParameterKeyName = @"ApplicationUrl";

        public override MvxShowViewModelRequest GetRequestFromXamlUri(Uri viewUri)
        {
            var parsed = viewUri.ParseQueryString();

            string queryString;
            if (!parsed.TryGetValue(QueryParameterKeyName, out queryString))
                throw new MvxException("Unable to find incoming MvxShowViewModelRequest");

            var text = Uri.UnescapeDataString(queryString);
            var converter = this.GetService<IMvxTextSerializer>();
            return converter.DeserializeObject<TeaShowViewModelRequest>(text);
        }

    }
}
